<?php
/**
 * Content module manages all articles, news, ...
 * @author Urban Soban
 * @package Content
 * @version 0.01a 
 * @copyright 2009 Urban Soban
 */
class ContentController extends ApplicationController {
	protected $AppName = "content";
	protected $IncludeFiles = array("content.class.php");
	public $UseApps = array(array("name" => "account"),
							array("name" => "mysql"),
							array("name" => "flags"),
							array("name" => "smarty", "method" => "getSmarty"));
	
	private $HTTP;

	function init(){
		$this->HTTP = new HTTPRequest();
		$URLPosition = SystemRegistry::getInstance()->get("url_position");
		
		$URL_Parameter = $this->HTTP->get($URLPosition);
		
		$ContentItems = array();
		
		if(is_numeric($URL_Parameter)){
			$ContentItem = $this->loadContent($URL_Parameter);
			
			if(!is_object($ContentItem)){
				return $ContentItem;	
			}
			
			$ContentItems[] = $ContentItem;
			
		
			$smarty = $this->UseApps["smarty"];
			$smarty->assign("content_items", $ContentItems);
		
			$this->ViewFile = "display_articles.tpl";
		}
		else {
			return $this;
		}
	} 

	/**
	 * Creates and returns ContentItem of given ID.
	 *
	 * @param int $id
	 * @return ContentItem
	 */
	function loadContent($id){
		if (!is_numeric($id)) {
			throw new FMSException("ID not numeric!");
		}
		
		$ContentItem = new ContentItem($this, $id);
		
		if($ContentItem->getID() == null){
			return $this->Language->get("article_not_found");
		}
		else {
			return $ContentItem;
		}
	}
	
	function loadArticleToTemplate($id){
		$ContentItem = $this->loadContent($id[0]);
		$content_items[] = $ContentItem;
		
		$Smarty = $this->UseApps["smarty"];
		$Smarty->assign("content_items", $content_items);
		$this->ViewFile = "display_articles.tpl";
		
		return true;
	}
	
	function addContentItem($title, $text, $allow_tags, $author, array $flags = array(), $id = null){
		$NewItem = new ContentItem($this, $id);
		
		if($NewItem->updateItem($title, $text, $author, $allow_tags, $flags)){
			return $NewItem;
		}
		
		return false;
	}
	
	/**
	 * Deletes item with given ID
	 *
	 * @param int $id
	 * @return boolean
	 */
	function deleteItem($id){
		$NewItem = $this->loadContent($id);
		
		if(is_object($NewItem)){
			$NewItem->deleteItem();
			return true;
		}
		
		return false;
	}
}

?>